<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>Monogo副本集 - 德国粗茶淡饭</title><meta name="Description" content="Monogo副本集"><meta property="og:title" content="Monogo副本集" />
<meta property="og:description" content="Monogo副本集" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.ctq6.cn/technology/mongo/monogo%E5%89%AF%E6%9C%AC%E9%9B%86/" />
<meta property="og:image" content="https://www.ctq6.cn/logo.png"/>
<meta property="article:published_time" content="2021-02-16T19:55:35+08:00" />
<meta property="article:modified_time" content="2021-02-16T19:55:35+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://www.ctq6.cn/logo.png"/>

<meta name="twitter:title" content="Monogo副本集"/>
<meta name="twitter:description" content="Monogo副本集"/>
<meta name="application-name" content="LoveIt">
<meta name="apple-mobile-web-app-title" content="LoveIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://www.ctq6.cn/technology/mongo/monogo%E5%89%AF%E6%9C%AC%E9%9B%86/" /><link rel="prev" href="https://www.ctq6.cn/technology/mongo/monogo%E5%AE%89%E8%A3%85/" /><link rel="next" href="https://www.ctq6.cn/technology/logging/efk%E6%97%A5%E5%BF%97%E5%B9%B3%E5%8F%B0%E9%83%A8%E7%BD%B2/" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Monogo副本集",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/www.ctq6.cn\/technology\/mongo\/monogo%E5%89%AF%E6%9C%AC%E9%9B%86\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/www.ctq6.cn\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "technology","keywords": "Mongo","wordcount":  2853 ,
        "url": "https:\/\/www.ctq6.cn\/technology\/mongo\/monogo%E5%89%AF%E6%9C%AC%E9%9B%86\/","datePublished": "2021-02-16T19:55:35+08:00","dateModified": "2021-02-16T19:55:35+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "https:\/\/www.ctq6.cn\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "MikelPan"
            },"description": "Monogo副本集"
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="德国粗茶淡饭"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>德国粗茶淡饭</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 生活 </a><a class="menu-item" href="/technology/"> 技术 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/MikelPan/Cnblog.git" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i>  </a><span class="menu-item delimiter"></span><a href="javascript:void(0);" class="menu-item language" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                        <select class="language-select" id="language-select-desktop" onchange="location = this.value;"><option value="/technology/mongo/monogo%E5%89%AF%E6%9C%AC%E9%9B%86/" selected>简体中文</option></select>
                    </a><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="德国粗茶淡饭"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>德国粗茶淡饭</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">生活</a><a class="menu-item" href="/technology/" title="">技术</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/MikelPan/Cnblog.git" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a><a href="javascript:void(0);" class="menu-item" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                    <select class="language-select" onchange="location = this.value;"><option value="/technology/mongo/monogo%E5%89%AF%E6%9C%AC%E9%9B%86/" selected>简体中文</option></select>
                </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="page single special"><h1 class="single-title animated pulse faster">Monogo副本集</h1><div class="content" id="content"><h3 id="mongo-副本集搭建">mongo 副本集搭建</h3>
<h4 id="mongodb主备仲裁的基本结构如下">MongoDB主备+仲裁的基本结构如下</h4>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://images2017.cnblogs.com/blog/907596/201711/907596-20171122210337196-303523786.png"
        data-srcset="https://images2017.cnblogs.com/blog/907596/201711/907596-20171122210337196-303523786.png, https://images2017.cnblogs.com/blog/907596/201711/907596-20171122210337196-303523786.png 1.5x, https://images2017.cnblogs.com/blog/907596/201711/907596-20171122210337196-303523786.png 2x"
        data-sizes="auto"
        alt="https://images2017.cnblogs.com/blog/907596/201711/907596-20171122210337196-303523786.png"
        title="https://images2017.cnblogs.com/blog/907596/201711/907596-20171122210337196-303523786.png" /></p>
<p>主节点（Primary）</p>
<blockquote>
<p>在复制集中，主节点是唯一能够接收写请求的节点。MongoDB在主节点进行写操作，并将这些操作记录到主节点的oplog中。而从节点将会从oplog复制到其本机，并将这些操作应用到自己的数据集上。（复制集最多只能拥有一个主节点）</p>
</blockquote>
<p>从节点（Secondaries）</p>
<blockquote>
<p>从节点通过应用主节点传来的数据变动操作来保持其数据集与主节点一致。从节点也可以通过增加额外参数配置来对应特殊需求。例如，从节点可以是non-voting或是priority 0.</p>
</blockquote>
<p>仲裁节点（ARBITER）</p>
<blockquote>
<p>仲裁节点即投票节点，其本身并不包含数据集，且也无法晋升为主节点。但是，旦当前的主节点不可用时，投票节点就会参与到新的主节点选举的投票中。仲裁节点使用最小的资源并且不要求硬件设备。投票节点的存在使得复制集可以以偶数个节点存在，而无需为复制集再新增节点 不要将投票节点运行在复制集的主节点或从节点机器上。 投票节点与其他 复制集节点的交流仅有：选举过程中的投票，心跳检测和配置数据。这些交互都是不加密的。</p>
</blockquote>
<p>心跳检测</p>
<blockquote>
<p>复制集成员每两秒向复制集中其他成员进行心跳检测。如果某个节点在10秒内没有返回，那么它将被标记为不可用。</p>
</blockquote>
<p>MongoDB副本集是有故障恢复功能的主从集群，由一个primary节点和一个或多个secondary节点组成：</p>
<blockquote>
<p>节点同步过程： Primary节点写入数据，Secondary通过读取Primary的oplog得到复制信息，开始复制数据并且将复制信息写入到自己的oplog。如果某个操作失败，则备份节点停止从当前数据源复制数据。如果某个备份节点由于某些原因挂掉了，当重新启动后，就会自动从oplog的最后一个操作开始同步，同步完成后，将信息写入自己的oplog，由于复制操作是先复制数据，复制完成后再写入oplog，有可能相同的操作会同步两份，不过MongoDB在设计之初就考虑到这个问题，将oplog的同一个操作执行多次，与执行一次的效果是一样的。</p>
</blockquote>
<p>通俗理解：当Primary节点完成数据操作后，Secondary会做出一系列的动作保证数据的同步：</p>
<ul>
<li>检查自己local库的oplog.rs集合，找出最近的时间戳。</li>
<li>检查Primary节点local库oplog.rs集合，找出大于此时间戳的记录。</li>
<li>将找到的记录插入到自己的oplog.rs集合中，并执行这些操作。</li>
</ul>
<p>副本集的同步和主从同步一样，都是异步同步的过程，不同的是副本集有个自动故障转移的功能。其原理是：slave端从primary端获取日志，然后在自己身上完全顺序的执行日志所记录的各种操作（该日志是不记录查询操作的），这个日志就是local数据 库中的oplog.rs表，默认在64位机器上这个表是比较大的，占磁盘大小的5%，oplog.rs的大小可以在启动参数中设 定：–oplogSize 1000,单位是M。</p>
<h4 id="部署过程如下">部署过程如下</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span><span class="lnt">116
</span><span class="lnt">117
</span><span class="lnt">118
</span><span class="lnt">119
</span><span class="lnt">120
</span><span class="lnt">121
</span><span class="lnt">122
</span><span class="lnt">123
</span><span class="lnt">124
</span><span class="lnt">125
</span><span class="lnt">126
</span><span class="lnt">127
</span><span class="lnt">128
</span><span class="lnt">129
</span><span class="lnt">130
</span><span class="lnt">131
</span><span class="lnt">132
</span><span class="lnt">133
</span><span class="lnt">134
</span><span class="lnt">135
</span><span class="lnt">136
</span><span class="lnt">137
</span><span class="lnt">138
</span><span class="lnt">139
</span><span class="lnt">140
</span><span class="lnt">141
</span><span class="lnt">142
</span><span class="lnt">143
</span><span class="lnt">144
</span><span class="lnt">145
</span><span class="lnt">146
</span><span class="lnt">147
</span><span class="lnt">148
</span><span class="lnt">149
</span><span class="lnt">150
</span><span class="lnt">151
</span><span class="lnt">152
</span><span class="lnt">153
</span><span class="lnt">154
</span><span class="lnt">155
</span><span class="lnt">156
</span><span class="lnt">157
</span><span class="lnt">158
</span><span class="lnt">159
</span><span class="lnt">160
</span><span class="lnt">161
</span><span class="lnt">162
</span><span class="lnt">163
</span><span class="lnt">164
</span><span class="lnt">165
</span><span class="lnt">166
</span><span class="lnt">167
</span><span class="lnt">168
</span><span class="lnt">169
</span><span class="lnt">170
</span><span class="lnt">171
</span><span class="lnt">172
</span><span class="lnt">173
</span><span class="lnt">174
</span><span class="lnt">175
</span><span class="lnt">176
</span><span class="lnt">177
</span><span class="lnt">178
</span><span class="lnt">179
</span><span class="lnt">180
</span><span class="lnt">181
</span><span class="lnt">182
</span><span class="lnt">183
</span><span class="lnt">184
</span><span class="lnt">185
</span><span class="lnt">186
</span><span class="lnt">187
</span><span class="lnt">188
</span><span class="lnt">189
</span><span class="lnt">190
</span><span class="lnt">191
</span><span class="lnt">192
</span><span class="lnt">193
</span><span class="lnt">194
</span><span class="lnt">195
</span><span class="lnt">196
</span><span class="lnt">197
</span><span class="lnt">198
</span><span class="lnt">199
</span><span class="lnt">200
</span><span class="lnt">201
</span><span class="lnt">202
</span><span class="lnt">203
</span><span class="lnt">204
</span><span class="lnt">205
</span><span class="lnt">206
</span><span class="lnt">207
</span><span class="lnt">208
</span><span class="lnt">209
</span><span class="lnt">210
</span><span class="lnt">211
</span><span class="lnt">212
</span><span class="lnt">213
</span><span class="lnt">214
</span><span class="lnt">215
</span><span class="lnt">216
</span><span class="lnt">217
</span><span class="lnt">218
</span><span class="lnt">219
</span><span class="lnt">220
</span><span class="lnt">221
</span><span class="lnt">222
</span><span class="lnt">223
</span><span class="lnt">224
</span><span class="lnt">225
</span><span class="lnt">226
</span><span class="lnt">227
</span><span class="lnt">228
</span><span class="lnt">229
</span><span class="lnt">230
</span><span class="lnt">231
</span><span class="lnt">232
</span><span class="lnt">233
</span><span class="lnt">234
</span><span class="lnt">235
</span><span class="lnt">236
</span><span class="lnt">237
</span><span class="lnt">238
</span><span class="lnt">239
</span><span class="lnt">240
</span><span class="lnt">241
</span><span class="lnt">242
</span><span class="lnt">243
</span><span class="lnt">244
</span><span class="lnt">245
</span><span class="lnt">246
</span><span class="lnt">247
</span><span class="lnt">248
</span><span class="lnt">249
</span><span class="lnt">250
</span><span class="lnt">251
</span><span class="lnt">252
</span><span class="lnt">253
</span><span class="lnt">254
</span><span class="lnt">255
</span><span class="lnt">256
</span><span class="lnt">257
</span><span class="lnt">258
</span><span class="lnt">259
</span><span class="lnt">260
</span><span class="lnt">261
</span><span class="lnt">262
</span><span class="lnt">263
</span><span class="lnt">264
</span><span class="lnt">265
</span><span class="lnt">266
</span><span class="lnt">267
</span><span class="lnt">268
</span><span class="lnt">269
</span><span class="lnt">270
</span><span class="lnt">271
</span><span class="lnt">272
</span><span class="lnt">273
</span><span class="lnt">274
</span><span class="lnt">275
</span><span class="lnt">276
</span><span class="lnt">277
</span><span class="lnt">278
</span><span class="lnt">279
</span><span class="lnt">280
</span><span class="lnt">281
</span><span class="lnt">282
</span><span class="lnt">283
</span><span class="lnt">284
</span><span class="lnt">285
</span><span class="lnt">286
</span><span class="lnt">287
</span><span class="lnt">288
</span><span class="lnt">289
</span><span class="lnt">290
</span><span class="lnt">291
</span><span class="lnt">292
</span><span class="lnt">293
</span><span class="lnt">294
</span><span class="lnt">295
</span><span class="lnt">296
</span><span class="lnt">297
</span><span class="lnt">298
</span><span class="lnt">299
</span><span class="lnt">300
</span><span class="lnt">301
</span><span class="lnt">302
</span><span class="lnt">303
</span><span class="lnt">304
</span><span class="lnt">305
</span><span class="lnt">306
</span><span class="lnt">307
</span><span class="lnt">308
</span><span class="lnt">309
</span><span class="lnt">310
</span><span class="lnt">311
</span><span class="lnt">312
</span><span class="lnt">313
</span><span class="lnt">314
</span><span class="lnt">315
</span><span class="lnt">316
</span><span class="lnt">317
</span><span class="lnt">318
</span><span class="lnt">319
</span><span class="lnt">320
</span><span class="lnt">321
</span><span class="lnt">322
</span><span class="lnt">323
</span><span class="lnt">324
</span><span class="lnt">325
</span><span class="lnt">326
</span><span class="lnt">327
</span><span class="lnt">328
</span><span class="lnt">329
</span><span class="lnt">330
</span><span class="lnt">331
</span><span class="lnt">332
</span><span class="lnt">333
</span><span class="lnt">334
</span><span class="lnt">335
</span><span class="lnt">336
</span><span class="lnt">337
</span><span class="lnt">338
</span><span class="lnt">339
</span><span class="lnt">340
</span><span class="lnt">341
</span><span class="lnt">342
</span><span class="lnt">343
</span><span class="lnt">344
</span><span class="lnt">345
</span><span class="lnt">346
</span><span class="lnt">347
</span><span class="lnt">348
</span><span class="lnt">349
</span><span class="lnt">350
</span><span class="lnt">351
</span><span class="lnt">352
</span><span class="lnt">353
</span><span class="lnt">354
</span><span class="lnt">355
</span><span class="lnt">356
</span><span class="lnt">357
</span><span class="lnt">358
</span><span class="lnt">359
</span><span class="lnt">360
</span><span class="lnt">361
</span><span class="lnt">362
</span><span class="lnt">363
</span><span class="lnt">364
</span><span class="lnt">365
</span><span class="lnt">366
</span><span class="lnt">367
</span><span class="lnt">368
</span><span class="lnt">369
</span><span class="lnt">370
</span><span class="lnt">371
</span><span class="lnt">372
</span><span class="lnt">373
</span><span class="lnt">374
</span><span class="lnt">375
</span><span class="lnt">376
</span><span class="lnt">377
</span><span class="lnt">378
</span><span class="lnt">379
</span><span class="lnt">380
</span><span class="lnt">381
</span><span class="lnt">382
</span><span class="lnt">383
</span><span class="lnt">384
</span><span class="lnt">385
</span><span class="lnt">386
</span><span class="lnt">387
</span><span class="lnt">388
</span><span class="lnt">389
</span><span class="lnt">390
</span><span class="lnt">391
</span><span class="lnt">392
</span><span class="lnt">393
</span><span class="lnt">394
</span><span class="lnt">395
</span><span class="lnt">396
</span><span class="lnt">397
</span><span class="lnt">398
</span><span class="lnt">399
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># 制作dockerfile 生产机器</span>
cat &gt; Dockerfile <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">FROM centos:7
</span><span class="s">RUN yum install wget vim net-tools htop -y \
</span><span class="s">    &amp;&amp; cp -r /etc/yum.repos.d /etc/yum.repos.d.bak \
</span><span class="s">    &amp;&amp; rm -f /etc/yum.repos.d/*.repo \
</span><span class="s">    &amp;&amp; wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo \
</span><span class="s">    &amp;&amp; wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo \
</span><span class="s">    &amp;&amp; yum clean all &amp;&amp; yum makecache \
</span><span class="s">    &amp;&amp; yum install -y openssh-server \
</span><span class="s">    &amp;&amp; mkdir /var/run/sshd \
</span><span class="s">    &amp;&amp; echo &#39;root:123456&#39; |chpasswd \
</span><span class="s">    &amp;&amp; sed -ri &#39;s/^#?PermitRootLogin\s+.*/PermitRootLogin yes/&#39; /etc/ssh/sshd_config \
</span><span class="s">    &amp;&amp; mkdir /root/.ssh \
</span><span class="s">    &amp;&amp; ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key 
</span><span class="s">EXPOSE 22
</span><span class="s">CMD [&#34;/usr/sbin/sshd&#34;, &#34;-D&#34;]
</span><span class="s">EOF</span>
docker build -t mongo_vm:v1 -f Dockerfile .
<span class="c1"># 创建机器</span>
<span class="k">for</span> i in <span class="sb">`</span>seq <span class="m">1</span> 3<span class="sb">`</span><span class="p">;</span><span class="k">do</span> docker run --rm -itd --privileged<span class="o">=</span><span class="nb">true</span>  --name mongo_vm_<span class="nv">$i</span> mongo_vm:v1 /usr/sbin/init<span class="p">;</span><span class="k">done</span>
<span class="c1"># 查询ip地址</span>
<span class="k">for</span> i in <span class="sb">`</span>seq <span class="m">1</span> 3<span class="sb">`</span><span class="p">;</span><span class="k">do</span> docker inspect mongo_vm_<span class="nv">$i</span> -f <span class="o">{{</span>.NetworkSettings.Networks.bridge.IPAddress<span class="o">}}</span><span class="p">;</span><span class="k">done</span>
<span class="c1"># 删除机器</span>
<span class="k">for</span> i in <span class="sb">`</span>seq <span class="m">1</span> 3<span class="sb">`</span><span class="p">;</span><span class="k">do</span> docker stop mongo_vm_<span class="nv">$i</span> mongo_vm:v1<span class="p">;</span><span class="k">done</span>
<span class="c1"># 服务器信息</span>
mongo01  172.17.0.3   Primary
mongo02  172.17.0.4   Secondary
mongo03  172.17.0.5   Secondary
<span class="c1"># 清除known_hosts</span>
<span class="k">for</span> i in <span class="sb">`</span>seq <span class="m">1</span> 3<span class="sb">`</span><span class="p">;</span><span class="k">do</span> docker <span class="nb">exec</span> -it mongo_vm_<span class="nv">$i</span> -- &gt; /root/.ssh/known_hosts<span class="p">;</span><span class="k">done</span>
<span class="c1"># 配置ssh-key</span>
<span class="k">for</span> i in <span class="sb">`</span>seq <span class="m">3</span> 5<span class="sb">`</span><span class="p">;</span><span class="k">do</span> ssh-copy-id root@172.17.0.<span class="nv">$i</span><span class="p">;</span><span class="k">done</span>
<span class="c1"># 安装ansible</span>
yum install -y ansible
cat &gt;&gt; /etc/ansible/hosts <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">[mongo]
</span><span class="s">172.17.0.3
</span><span class="s">172.17.0.4
</span><span class="s">172.17.0.5
</span><span class="s">EOF</span>
<span class="c1"># 编写ansible批量安装脚本</span>
cat &gt; deploy.yml <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">---
</span><span class="s">- hosts: mongo
</span><span class="s">  remote_user: root
</span><span class="s">  gather_facts: false
</span><span class="s">  tasks:
</span><span class="s">    - name: configure hosts
</span><span class="s">      shell: |
</span><span class="s">        cat &gt;&gt; /etc/hosts &lt;&lt;- &#39;EOF</span><span class="s1">&#39;
</span><span class="s1">        # mongo
</span><span class="s1">        172.17.0.3 mongo01
</span><span class="s1">        172.17.0.4 mongo02
</span><span class="s1">        172.17.0.5 mongo03
</span><span class="s1">        EOF
</span><span class="s1">      tags:
</span><span class="s1">      - install
</span><span class="s1">
</span><span class="s1">    - name: download mongo
</span><span class="s1">      shell: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.4.tgz -P /usr/local/src
</span><span class="s1">      tags:
</span><span class="s1">      - install
</span><span class="s1">    
</span><span class="s1">    - name: unarchive
</span><span class="s1">      unarchive:
</span><span class="s1">        src: /usr/local/src/mongodb-linux-x86_64-4.0.4.tgz
</span><span class="s1">        dest: /usr/local/src
</span><span class="s1">        mode: 0755
</span><span class="s1">        copy: no
</span><span class="s1">      tags:
</span><span class="s1">      - install
</span><span class="s1">
</span><span class="s1">    - name: mkdir dir
</span><span class="s1">      file:
</span><span class="s1">        path: &#34;{{ item }}&#34;
</span><span class="s1">        state: directory
</span><span class="s1">      with_items:
</span><span class="s1">      - &#34;/usr/local/mongo&#34;
</span><span class="s1">      - &#34;/data/mongo/db&#34;
</span><span class="s1">      tags:
</span><span class="s1">      - install
</span><span class="s1">
</span><span class="s1">    - name: create log file
</span><span class="s1">      file:
</span><span class="s1">        path: &#34;{{ item }}/mongod.log&#34;
</span><span class="s1">        state: touch
</span><span class="s1">      with_items:
</span><span class="s1">      - &#34;/data/mongo&#34;
</span><span class="s1">      tags:
</span><span class="s1">      - log
</span><span class="s1">      - install
</span><span class="s1">
</span><span class="s1">    - name: copy files
</span><span class="s1">      shell: mv &#34;{{ item }}&#34;/* /usr/local/mongo/
</span><span class="s1">      with_items:
</span><span class="s1">      - &#34;/usr/local/src/mongodb-linux-x86_64-4.0.4&#34;
</span><span class="s1">      tags:
</span><span class="s1">      - install
</span><span class="s1">
</span><span class="s1">    - name: configure env 
</span><span class="s1">      shell: |
</span><span class="s1">        echo &#39;</span><span class="nb">export</span> <span class="nv">PATH</span><span class="o">=</span>/usr/local/mongo/bin:<span class="nv">$PATH</span><span class="s1">&#39; &gt;&gt; /etc/profile
</span><span class="s1">        source /etc/profile
</span><span class="s1">      tags:
</span><span class="s1">      - env
</span><span class="s1">      - install
</span><span class="s1">
</span><span class="s1">    - name: configure conf file
</span><span class="s1">      shell: |
</span><span class="s1">        cat &gt; /data/mongo/mongodb.cnf &lt;&lt;- &#39;</span>EOF<span class="s1">&#39;
</span><span class="s1">        systemLog:
</span><span class="s1">          destination: file
</span><span class="s1">          logAppend: true
</span><span class="s1">          path: /data/mongo/mongod.log
</span><span class="s1">        net:
</span><span class="s1">          port: 27017
</span><span class="s1">          bindIp: 0.0.0.0
</span><span class="s1">        storage:
</span><span class="s1">          dbPath: /data/mongo/db
</span><span class="s1">        journal:
</span><span class="s1">          enabled: true
</span><span class="s1">        processManagement:
</span><span class="s1">          fork: true
</span><span class="s1">          pidFilePath: /data/mongo/mongod.pid
</span><span class="s1">        security:
</span><span class="s1">          authorization: enabled
</span><span class="s1">          keyFile: data/mongo/keyfile
</span><span class="s1">          clusterAuthMode: keyFile
</span><span class="s1">        replication:
</span><span class="s1">          replSetName: rs0
</span><span class="s1">        EOF
</span><span class="s1">      tags:
</span><span class="s1">      - conf
</span><span class="s1">      - install
</span><span class="s1">
</span><span class="s1">    - name: reboot to start
</span><span class="s1">      shell: |
</span><span class="s1">        cat &gt; /etc/systemd/system/mongod.service &lt;&lt;- &#39;</span>EOF<span class="s1">&#39;
</span><span class="s1">        [Unit]
</span><span class="s1">        Description=mongodb
</span><span class="s1">        After=network.target remote-fs.target nss-lookup.target
</span><span class="s1">
</span><span class="s1">        [Service]
</span><span class="s1">        Type=forking
</span><span class="s1">        ExecStart=/usr/local/mongo/bin/mongod --config /data/mongo/mongodb.cnf
</span><span class="s1">        ExecReload=/bin/kill -s HUP $MAINPID
</span><span class="s1">        ExecStop=/usr/local/mongo/bin/mongod --shutdown --config /data/mongo/mongodb.cnf
</span><span class="s1">        PrivateTmp=true
</span><span class="s1">        [Install]
</span><span class="s1">        WantedBy=multi-user.target
</span><span class="s1">        EOF
</span><span class="s1">        systemctl enable mongod.servcie
</span><span class="s1">        systemctl start mongod
</span><span class="s1">      tags:
</span><span class="s1">      - start
</span><span class="s1">      - install
</span><span class="s1">EOF
</span><span class="s1">#ansible-playbook deploy.yml --tags start
</span><span class="s1">ansible-playbook deploy.yml --tags install
</span><span class="s1"># 配置mongo 副本集
</span><span class="s1">## 登陆主节点
</span><span class="s1">mongo
</span><span class="s1">use admin
</span><span class="s1">rs.initiate({_id:&#39;</span>rs0<span class="s1">&#39;,members: [{ _id: 0 , host: &#34;mongo01:27017&#34;}]})
</span><span class="s1">## 添加次节点
</span><span class="s1">rs.add(&#39;</span>mongo02:27017<span class="s1">&#39;)
</span><span class="s1">rs.add(&#39;</span>mongo03:27017<span class="s1">&#39;)
</span><span class="s1">## 查看配置
</span><span class="s1">rs.conf()
</span><span class="s1">{
</span><span class="s1">	&#34;_id&#34; : &#34;rs0&#34;,
</span><span class="s1">	&#34;version&#34; : 3,
</span><span class="s1">	&#34;protocolVersion&#34; : NumberLong(1),
</span><span class="s1">	&#34;writeConcernMajorityJournalDefault&#34; : true,
</span><span class="s1">	&#34;members&#34; : [
</span><span class="s1">		{
</span><span class="s1">			&#34;_id&#34; : 0,
</span><span class="s1">			&#34;host&#34; : &#34;mongo01:27017&#34;,
</span><span class="s1">			&#34;arbiterOnly&#34; : false,
</span><span class="s1">			&#34;buildIndexes&#34; : true,
</span><span class="s1">			&#34;hidden&#34; : false,
</span><span class="s1">			&#34;priority&#34; : 1,
</span><span class="s1">			&#34;tags&#34; : {
</span><span class="s1">			},
</span><span class="s1">			&#34;slaveDelay&#34; : NumberLong(0),
</span><span class="s1">			&#34;votes&#34; : 1
</span><span class="s1">		},
</span><span class="s1">		{
</span><span class="s1">			&#34;_id&#34; : 1,
</span><span class="s1">			&#34;host&#34; : &#34;mongo02:27017&#34;,
</span><span class="s1">			&#34;arbiterOnly&#34; : false,
</span><span class="s1">			&#34;buildIndexes&#34; : true,
</span><span class="s1">			&#34;hidden&#34; : false,
</span><span class="s1">			&#34;priority&#34; : 1,
</span><span class="s1">			&#34;tags&#34; : {
</span><span class="s1">			},
</span><span class="s1">			&#34;slaveDelay&#34; : NumberLong(0),
</span><span class="s1">			&#34;votes&#34; : 1
</span><span class="s1">		},
</span><span class="s1">		{
</span><span class="s1">			&#34;_id&#34; : 2,
</span><span class="s1">			&#34;host&#34; : &#34;mongo03:27017&#34;,
</span><span class="s1">			&#34;arbiterOnly&#34; : false,
</span><span class="s1">			&#34;buildIndexes&#34; : true,
</span><span class="s1">			&#34;hidden&#34; : false,
</span><span class="s1">			&#34;priority&#34; : 1,
</span><span class="s1">			&#34;tags&#34; : {
</span><span class="s1">			},
</span><span class="s1">			&#34;slaveDelay&#34; : NumberLong(0),
</span><span class="s1">			&#34;votes&#34; : 1
</span><span class="s1">		}
</span><span class="s1">	],
</span><span class="s1">	&#34;settings&#34; : {
</span><span class="s1">		&#34;chainingAllowed&#34; : true,
</span><span class="s1">		&#34;heartbeatIntervalMillis&#34; : 2000,
</span><span class="s1">		&#34;heartbeatTimeoutSecs&#34; : 10,
</span><span class="s1">		&#34;electionTimeoutMillis&#34; : 10000,
</span><span class="s1">		&#34;catchUpTimeoutMillis&#34; : -1,
</span><span class="s1">		&#34;catchUpTakeoverDelayMillis&#34; : 30000,
</span><span class="s1">		&#34;getLastErrorModes&#34; : {
</span><span class="s1">		},
</span><span class="s1">		&#34;getLastErrorDefaults&#34; : {
</span><span class="s1">			&#34;w&#34; : 1,
</span><span class="s1">			&#34;wtimeout&#34; : 0
</span><span class="s1">		},
</span><span class="s1">		&#34;replicaSetId&#34; : ObjectId(&#34;5fa74da8ebc391934052afbc&#34;)
</span><span class="s1">	}
</span><span class="s1">}
</span><span class="s1">## 查看各节点状态
</span><span class="s1">rs.status()
</span><span class="s1">一个主节点，两个从节点
</span><span class="s1">{
</span><span class="s1">	&#34;set&#34; : &#34;rs0&#34;,
</span><span class="s1">	&#34;date&#34; : ISODate(&#34;2020-11-08T01:49:51.705Z&#34;),
</span><span class="s1">	&#34;myState&#34; : 1,
</span><span class="s1">	&#34;term&#34; : NumberLong(1),
</span><span class="s1">	&#34;syncingTo&#34; : &#34;&#34;,
</span><span class="s1">	&#34;syncSourceHost&#34; : &#34;&#34;,
</span><span class="s1">	&#34;syncSourceId&#34; : -1,
</span><span class="s1">	&#34;heartbeatIntervalMillis&#34; : NumberLong(2000),
</span><span class="s1">	&#34;optimes&#34; : {
</span><span class="s1">		&#34;lastCommittedOpTime&#34; : {
</span><span class="s1">			&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">			&#34;t&#34; : NumberLong(1)
</span><span class="s1">		},
</span><span class="s1">		&#34;readConcernMajorityOpTime&#34; : {
</span><span class="s1">			&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">			&#34;t&#34; : NumberLong(1)
</span><span class="s1">		},
</span><span class="s1">		&#34;appliedOpTime&#34; : {
</span><span class="s1">			&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">			&#34;t&#34; : NumberLong(1)
</span><span class="s1">		},
</span><span class="s1">		&#34;durableOpTime&#34; : {
</span><span class="s1">			&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">			&#34;t&#34; : NumberLong(1)
</span><span class="s1">		}
</span><span class="s1">	},
</span><span class="s1">	&#34;lastStableCheckpointTimestamp&#34; : Timestamp(1604800155, 1),
</span><span class="s1">	&#34;members&#34; : [
</span><span class="s1">		{
</span><span class="s1">			&#34;_id&#34; : 0,
</span><span class="s1">			&#34;name&#34; : &#34;mongo01:27017&#34;,
</span><span class="s1">			&#34;health&#34; : 1,
</span><span class="s1">			&#34;state&#34; : 1,
</span><span class="s1">			&#34;stateStr&#34; : &#34;PRIMARY&#34;,
</span><span class="s1">			&#34;uptime&#34; : 366,
</span><span class="s1">			&#34;optime&#34; : {
</span><span class="s1">				&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">				&#34;t&#34; : NumberLong(1)
</span><span class="s1">			},
</span><span class="s1">			&#34;optimeDate&#34; : ISODate(&#34;2020-11-08T01:49:45Z&#34;),
</span><span class="s1">			&#34;syncingTo&#34; : &#34;&#34;,
</span><span class="s1">			&#34;syncSourceHost&#34; : &#34;&#34;,
</span><span class="s1">			&#34;syncSourceId&#34; : -1,
</span><span class="s1">			&#34;infoMessage&#34; : &#34;&#34;,
</span><span class="s1">			&#34;electionTime&#34; : Timestamp(1604799913, 1),
</span><span class="s1">			&#34;electionDate&#34; : ISODate(&#34;2020-11-08T01:45:13Z&#34;),
</span><span class="s1">			&#34;configVersion&#34; : 3,
</span><span class="s1">			&#34;self&#34; : true,
</span><span class="s1">			&#34;lastHeartbeatMessage&#34; : &#34;&#34;
</span><span class="s1">		},
</span><span class="s1">		{
</span><span class="s1">			&#34;_id&#34; : 1,
</span><span class="s1">			&#34;name&#34; : &#34;mongo02:27017&#34;,
</span><span class="s1">			&#34;health&#34; : 1,
</span><span class="s1">			&#34;state&#34; : 2,
</span><span class="s1">			&#34;stateStr&#34; : &#34;SECONDARY&#34;,
</span><span class="s1">			&#34;uptime&#34; : 184,
</span><span class="s1">			&#34;optime&#34; : {
</span><span class="s1">				&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">				&#34;t&#34; : NumberLong(1)
</span><span class="s1">			},
</span><span class="s1">			&#34;optimeDurable&#34; : {
</span><span class="s1">				&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">				&#34;t&#34; : NumberLong(1)
</span><span class="s1">			},
</span><span class="s1">			&#34;optimeDate&#34; : ISODate(&#34;2020-11-08T01:49:45Z&#34;),
</span><span class="s1">			&#34;optimeDurableDate&#34; : ISODate(&#34;2020-11-08T01:49:45Z&#34;),
</span><span class="s1">			&#34;lastHeartbeat&#34; : ISODate(&#34;2020-11-08T01:49:51.380Z&#34;),
</span><span class="s1">			&#34;lastHeartbeatRecv&#34; : ISODate(&#34;2020-11-08T01:49:51.381Z&#34;),
</span><span class="s1">			&#34;pingMs&#34; : NumberLong(0),
</span><span class="s1">			&#34;lastHeartbeatMessage&#34; : &#34;&#34;,
</span><span class="s1">			&#34;syncingTo&#34; : &#34;mongo01:27017&#34;,
</span><span class="s1">			&#34;syncSourceHost&#34; : &#34;mongo01:27017&#34;,
</span><span class="s1">			&#34;syncSourceId&#34; : 0,
</span><span class="s1">			&#34;infoMessage&#34; : &#34;&#34;,
</span><span class="s1">			&#34;configVersion&#34; : 3
</span><span class="s1">		},
</span><span class="s1">		{
</span><span class="s1">			&#34;_id&#34; : 2,
</span><span class="s1">			&#34;name&#34; : &#34;mongo03:27017&#34;,
</span><span class="s1">			&#34;health&#34; : 1,
</span><span class="s1">			&#34;state&#34; : 2,
</span><span class="s1">			&#34;stateStr&#34; : &#34;SECONDARY&#34;,
</span><span class="s1">			&#34;uptime&#34; : 112,
</span><span class="s1">			&#34;optime&#34; : {
</span><span class="s1">				&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">				&#34;t&#34; : NumberLong(1)
</span><span class="s1">			},
</span><span class="s1">			&#34;optimeDurable&#34; : {
</span><span class="s1">				&#34;ts&#34; : Timestamp(1604800185, 1),
</span><span class="s1">				&#34;t&#34; : NumberLong(1)
</span><span class="s1">			},
</span><span class="s1">			&#34;optimeDate&#34; : ISODate(&#34;2020-11-08T01:49:45Z&#34;),
</span><span class="s1">			&#34;optimeDurableDate&#34; : ISODate(&#34;2020-11-08T01:49:45Z&#34;),
</span><span class="s1">			&#34;lastHeartbeat&#34; : ISODate(&#34;2020-11-08T01:49:51.570Z&#34;),
</span><span class="s1">			&#34;lastHeartbeatRecv&#34; : ISODate(&#34;2020-11-08T01:49:50.191Z&#34;),
</span><span class="s1">			&#34;pingMs&#34; : NumberLong(0),
</span><span class="s1">			&#34;lastHeartbeatMessage&#34; : &#34;&#34;,
</span><span class="s1">			&#34;syncingTo&#34; : &#34;mongo02:27017&#34;,
</span><span class="s1">			&#34;syncSourceHost&#34; : &#34;mongo02:27017&#34;,
</span><span class="s1">			&#34;syncSourceId&#34; : 1,
</span><span class="s1">			&#34;infoMessage&#34; : &#34;&#34;,
</span><span class="s1">			&#34;configVersion&#34; : 3
</span><span class="s1">		}
</span><span class="s1">	],
</span><span class="s1">	&#34;ok&#34; : 1,
</span><span class="s1">	&#34;operationTime&#34; : Timestamp(1604800185, 1),
</span><span class="s1">	&#34;$clusterTime&#34; : {
</span><span class="s1">		&#34;clusterTime&#34; : Timestamp(1604800185, 1),
</span><span class="s1">		&#34;signature&#34; : {
</span><span class="s1">			&#34;hash&#34; : BinData(0,&#34;AAAAAAAAAAAAAAAAAAAAAAAAAAA=&#34;),
</span><span class="s1">			&#34;keyId&#34; : NumberLong(0)
</span><span class="s1">		}
</span><span class="s1">	}
</span><span class="s1">}
</span><span class="s1">## 配置从节点读
</span><span class="s1">### 直接查询报错
</span><span class="s1">rs0:SECONDARY&gt; show dbs
</span><span class="s1">2020-11-08T01:54:54.250+0000 E QUERY    [js] Error: listDatabases failed:{
</span><span class="s1">	&#34;operationTime&#34; : Timestamp(1604800485, 1),
</span><span class="s1">	&#34;ok&#34; : 0,
</span><span class="s1">	&#34;errmsg&#34; : &#34;not master and slaveOk=false&#34;,
</span><span class="s1">	&#34;code&#34; : 13435,
</span><span class="s1">	&#34;codeName&#34; : &#34;NotMasterNoSlaveOk&#34;,
</span><span class="s1">	&#34;$clusterTime&#34; : {
</span><span class="s1">		&#34;clusterTime&#34; : Timestamp(1604800485, 1),
</span><span class="s1">		&#34;signature&#34; : {
</span><span class="s1">			&#34;hash&#34; : BinData(0,&#34;AAAAAAAAAAAAAAAAAAAAAAAAAAA=&#34;),
</span><span class="s1">			&#34;keyId&#34; : NumberLong(0)
</span><span class="s1">		}
</span><span class="s1">	}
</span><span class="s1">} :
</span><span class="s1">_getErrorWithCode@src/mongo/shell/utils.js:25:13
</span><span class="s1">Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1
</span><span class="s1">shellHelper.show@src/mongo/shell/utils.js:876:19
</span><span class="s1">shellHelper@src/mongo/shell/utils.js:766:15
</span><span class="s1">@(shellhelp2):1:1
</span><span class="s1">### 开启slave读写
</span><span class="s1">rs0:SECONDARY&gt; rs.slaveOk()
</span><span class="s1">rs0:SECONDARY&gt; show dbs
</span><span class="s1">admin   0.000GB
</span><span class="s1">config  0.000GB
</span><span class="s1">local   0.000GB
</span><span class="s1">## 配置安全访问
</span><span class="s1">### 主节点创建账号
</span><span class="s1">use admin
</span><span class="s1">db.createUser({
</span><span class="s1">  user : &#39;</span>admin<span class="s1">&#39;,
</span><span class="s1">  pwd : &#39;</span>123456<span class="s1">&#39;,
</span><span class="s1">  roles : [
</span><span class="s1">    &#39;</span>root<span class="s1">&#39;
</span><span class="s1">  ]
</span><span class="s1">})
</span><span class="s1">### 生成keyfile
</span><span class="s1">openssl rand -base64 90 -out /data/mongo/keyfile
</span><span class="s1">### 并复制到其它两个节点
</span><span class="s1">scp /data/mongo/keyfile  mongo02:/data/mongo/
</span><span class="s1">scp /data/mongo/keyfile  mongo03:/data/mongo/
</span><span class="s1">chmod 600 /data/mongo/keyfile
</span><span class="s1">### 修改配置文件
</span><span class="s1">security:
</span><span class="s1">   authorization: &#34;enabled&#34; 
</span><span class="s1">   keyFile: &#39;</span>/data/mongo/keyfile<span class="err">&#39;</span>
   clusterAuthMode: <span class="s2">&#34;keyFile&#34;</span>
<span class="c1">### 重启</span>
systemctl restart mongod
</code></pre></td></tr></table>
</div>
</div><h5 id="故障模拟">故障模拟</h5>
<h6 id="主节点宕机次节点选主">主节点宕机，次节点选主</h6>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># 停止其中一台节点</span>
<span class="c1">## 停止一台</span>
systemctl stop mongo01
<span class="c1">## 登陆查看节点状态</span>
rs.status<span class="o">()</span>
</code></pre></td></tr></table>
</div>
</div></div><div id="comments"></div></div></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2019 - 2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">mikel pan</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span><span class="icp-splitter">&nbsp;|&nbsp;</span><br class="icp-br"/>
                    <span class="icp"><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备2021047442号</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/css/lightgallery.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.37.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/algoliasearch@4.2.0/dist/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/js/lightgallery.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-thumbnail.js@1.2.0/dist/lg-thumbnail.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-zoom.js@1.2.0/dist/lg-zoom.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true},"search":{"algoliaAppID":"REQJX89W85","algoliaIndex":"index.zh-cn","algoliaSearchKey":"63fa048de9b35627f46672e95abc14df","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
